<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item> <i class="el-icon-lx-cascades"></i> 到货管理 </el-breadcrumb-item>
                <el-breadcrumb-item>货物回执单</el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-button type="danger" icon="el-icon-delete" class="handle-del mr10" @click="delAllSelection">批量删除</el-button>
                司机姓名：<el-input
                    v-model="query.driverName"
                    placeholder="司机姓名"
                    class="handle-input mr10"
                    style="width: 200px"
                ></el-input>
                客户手机号：<el-input v-model="query.consigneePhone" placeholder="客户手机号" class="handle-input mr10" style="width: 200px"></el-input>
                货运单编号：<el-input v-model="query.waybillNumber" placeholder="货运单编号" class="handle-input mr10" style="width: 200px"></el-input>
                起运日期：<el-date-picker value-format="yyyy-MM-dd HH:mm:ss" type="datetime" v-model="query.startTime" placeholder="起运时间" class="handle-input mr10" style="width: 200px"></el-date-picker>
                <el-date-picker value-format="yyyy-MM-dd HH:mm:ss" type="datetime" v-model="query.endTime" placeholder="到货时间" class="handle-input mr10" style="width: 200px"></el-date-picker>
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
            </div>
            <el-table
                :data="tableData"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
                @selection-change="handleSelectionChange"
                :row-key="getRowKeys"
            >
                <el-table-column type="selection" width="55" align="center" :reserve-selection="true"></el-table-column>
                <el-table-column prop="id" label="ID" width="55" align="center"></el-table-column>
                <el-table-column prop="waybillNumber" label="货运单编号"></el-table-column>
                <el-table-column prop="startTime" label="起运时间" > </el-table-column>
                <el-table-column prop="endTime" label="到货时间"> </el-table-column>
                <el-table-column prop="freight" label="运费"> </el-table-column>
                <el-table-column prop="premium" label="保险费"> </el-table-column>
                <el-table-column prop="consignee" label="收货人"> </el-table-column>
                <el-table-column prop="consigneePhone" label="收货人电话"> </el-table-column>
                <el-table-column prop="driverName" label="司机姓名"> </el-table-column>
                <el-table-column prop="status" label="是否结算"> </el-table-column>
                <el-table-column prop="receivingAcceptance" label="收货验收"> </el-table-column>
                <el-table-column prop="customerAcceptance" label="客户验收"> </el-table-column>
                

                <el-table-column label="操作" width="200" align="center">
                    <template slot-scope="scope">
                        <!-- <el-button type="text" icon="el-icon-lx-roundadd" @click="addTitle(scope.$index, scope.row)">添加试题</el-button> -->
                        <el-button type="text" icon="el-icon-edit" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                        <el-button type="text" icon="el-icon-delete" class="red" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <Pagination v-bind:child-msg="pageparm" @callFather="callFather"></Pagination>
                
            </div>
        </div>

        <!-- 编辑弹出框 -->
        <el-dialog title="编辑" :visible.sync="editVisible" width="30%">
            <el-form ref="form" :model="form" label-width="70px">
                <el-form-item label="收货验收">
                    <el-input v-model="form.receivingAcceptance"></el-input>
                </el-form-item>
                <el-form-item label="客户验收">
                    <el-input v-model="form.customerAcceptance"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveEdit">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import { fetchData } from '../../api/index';
import Pagination from '../common/Pagination.vue';
export default {
    name: 'basetable',
    data() {
        return {
            query: {
                driverName: '',
                consigneePhone: '',
                waybillNumber: '',
                startTime: '',
                endTime:''
            },
            formInline: {
                page: 1,
                limit: 5
            },
            // 分页参数
            pageparm: {
                currentPage: 1,
                pageSize: 5,
                total: 5
            },
            tableData: [],
            current: '',
            multipleSelection: [],
            delList: [],
            editVisible: false,
            pageTotal: 0,
            form: {},
            idx: -1,
            id: -1
        };
    },
    // 注册组件
    components: {
        Pagination
    },
    created() {
        this.getData(this.formInline);
        //console.log(this.$route.query.id);
    },
    methods: {
        // 获取 培训项目数据
        getData(parameter) {
            this.pageparm.currentPage = this.formInline.page;
            this.pageparm.pageSize = this.formInline.limit;
            console.log(this.query.startTime);

            this.$axios
                .post('/api/receipt/freightReceipt/findAll/' + this.pageparm.currentPage + '/' + this.pageparm.pageSize, this.query)
                .then((res) => {
                    console.log(res);
                    this.tableData = res.data.data.list;
                    
                    this.pageparm.total = res.data.data.total;
                })
                .catch((err) => {
                    console.log(err);
                });
        },
        // 分页插件事件
        callFather(parm) {
            //子传父 当前页和每页大小
            this.formInline.page = parm.currentPage;
            this.formInline.limit = parm.pageSize;
            this.getData(this.formInline);
        },
        getRowKeys(row) {
            return row.id;
        },
        // 触发搜索按钮
        handleSearch() {
            this.$set(this.pageparm, 'currentPage', 1);
            this.getData();
        },
        // 删除操作
        handleDelete(index, row) {
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            })
                .then(() => {
                    this.$axios
                        .delete('/api/receipt/freightReceipt/delete/' + row.id)
                        .then((res) => {
                            console.log(res);
                            if (res.data.code == 200) {
                                this.$message({
                                    type: 'success',
                                    message: '数据已删除!'
                                });
                                //重新获取当前页面数据重新渲染列表！
                                this.getData(this.pageparm);
                            } else {
                                this.$message({
                                    type: 'info',
                                    message: res.data.msg
                                });
                            }
                        })
                        .catch((err) => {
                            //this.$message.error('数据删除失败，请稍后再试！')
                            console.log(err);
                        });
                })
                .catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
        },
        // 多选操作
        handleSelectionChange(val) {
            //console.log(val);
            this.multipleSelection = val;
        },
        delAllSelection() {
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            })
                .then(() => {
                    //console.log(this.multipleSelection);
                    const length = this.multipleSelection.length;
                    let str = '';
                    this.delList = this.delList.concat(this.multipleSelection);
                    for (let i = 0; i < length; i++) {
                        str += this.multipleSelection[i].trainingname + ' ';
                        console.log(this.multipleSelection[i].id);
                        this.$axios
                            .delete('/api/trainings/deleteOne/' + this.multipleSelection[i].id)
                            .then((res) => {
                                console.log(res);
                                if (res.data.code == 200) {
                                    this.$message({
                                        type: 'success',
                                        message: '数据已删除!'
                                    });
                                    //重新获取当前页面数据重新渲染列表！
                                    this.getData(this.pageparm);
                                } else {
                                    this.$message({
                                        type: 'info',
                                        message: res.data.msg
                                    });
                                }
                            })
                            .catch((err) => {
                                //this.$message.error('数据删除失败，请稍后再试！')
                                console.log(err);
                            });
                        //console.log(str);
                    }
                    this.$message.error(`删除了${str}`);
                    this.multipleSelection = [];
                })
                .catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
        },
        // 编辑操作
        handleEdit(index, row) {
            this.idx = index;
            this.form = row;
            this.editVisible = true;
        },
        // 保存编辑
        saveEdit() {
            this.editVisible = false;
            this.$message.success(`修改第 ${this.idx + 1} 行成功`);
            //this.$set(this.tableData, this.idx, this.form);
            this.$axios
                .post('/api/trainings/updateById/', this.form)
                .then((res) => {
                    if (res.data.code == 200) {
                        this.getData(this.pageparm);
                        this.$message({
                            type: 'success',
                            message: '数据修改成功！'
                        });
                        this.editFormVisible = false;
                    } else {
                        this.$message({
                            type: 'info',
                            message: res.data.msg
                        });
                        this.editFormVisible = false;
                    }
                })
                .catch();
        },
        addTitle(index, row){
            console.log(row);
            localStorage.setItem('trainingid',row.id);
            this.$route.push('/createTitle');
        }
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
